/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package asfabdesk.util;

import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
//import javax.servlet.http.HttpServletResponse;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.util.JRLoader;
import net.sf.jasperreports.view.JasperViewer;

/**
 *
 * @author frederico
 */
public class RelatorioUtil {

    public JasperPrint imprimir(String nomeDoRelatorio, String tituloDoArquivoGerado, List<HashMap<String, String>> lista, Map<String, String> parametros) {
        try {
//            byte[] arquivo = new byte[0];
            JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(lista);
            JasperReport jasper = obterRelatorio(nomeDoRelatorio);
            System.out.println("NOME DO JASPER:" + jasper.getName());
            JasperPrint print = JasperFillManager.fillReport(jasper, parametros, ds);
            JasperViewer viewer = new JasperViewer(print, false);
            viewer.setDefaultCloseOperation(1);
            viewer.setVisible(true);

//            arquivo = JasperExportManager.exportReportToPdf(print);


//            response.setContentType("application/pdf");
//            response.setContentLength(arquivo.length);
//            String arq = tituloDoArquivoGerado + ".pdf";
//            response.setHeader("Content-Disposition", arq);

//            OutputStream saida = new OutputStream() {
//
//                @Override
//                public void write(int b) throws IOException {
//                    throw new UnsupportedOperationException("Not supported yet.");
//                }
//            };
//
//            saida.write(arquivo, 0, arquivo.length);
//            saida.flush();
//            saida.close();
            return print;
        } catch (Exception ex) {
            ex.printStackTrace();
        } 
        return null;
    }

    private static JasperReport obterRelatorio(String nome) throws JRException {
        Class classpath = new RelatorioUtil().getClass();
        URL url = classpath.getResource("/asfabdesk/relatorios/" + nome + ".jasper");
        return (JasperReport) JRLoader.loadObject(url);
    }

//     public JasperPrint relatorio( String jasperRel) throws Exception {
//        JasperPrint rel = null;
//        try {
//
//            JRResultSetDataSource jrRs = new JRResultSetDataSource(rs);
//            HashMap map = new HashMap();
//
//            rel = JasperFillManager.fillReport(arquivoJasper, map, jrRs);
//        } catch (JRException jre) {
//            jre.printStackTrace();
//            JOptionPane.showMessageDialog(null, "ARQUIVO NÃO ENCONTRADO!!!!!!!", "Atenção", 3);
//        } catch (SQLException e) {
//            JOptionPane.showMessageDialog(null, "ERRO NO COMANDO SQL " + e.getMessage());
//            e.printStackTrace();
//        }
//        return rel;
//    }
}
